package com.cap.dreamcircle.View;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.DreamLabelBean;
import com.cap.dreamcircle.Model.Bean.DreamLabelEntity;
import com.cap.dreamcircle.Model.IDreamModel;
import com.cap.dreamcircle.Model.Impl.DreamModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.View.Adapter.SearchDreamLabelAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liucxu on 2017/12/22.
 */

public class SearchDreamLabelActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_cancel_label)
    TextView tv_cancel_label;
    @BindView(R.id.edt_query_label)
    EditText edt_query_label;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;

    private List<DreamLabelEntity> datas = new ArrayList<>();
    private SearchDreamLabelAdapter adapter;
    private IDreamModel model;

    @Override
    public int getLayoutId() {
        return R.layout.activity_dream_label_search;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        tv_title.setText("解梦");
        adapter = new SearchDreamLabelAdapter(this, datas);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(adapter);
    }

    @Override
    public void setupData() {
        if (model == null) {
            model = new DreamModelImpl();
        }
        edt_query_label.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    tv_cancel_label.setVisibility(View.GONE);
                    datas.clear();
                    adapter.setDatas(datas);
                } else {
                    tv_cancel_label.setVisibility(View.VISIBLE);
                    queryDreamTagsByKeywords();
                }
            }
        });
    }

    private void queryDreamTagsByKeywords() {
        model.QueryTagsByKeyWords(edt_query_label.getText().toString()).subscribe(new CommonSubscriber<DreamLabelBean>() {
            @Override
            public void onSuccess(DreamLabelBean response) {
                if (!((response.getTagArray() == null) || (response.getTagArray().size() == 0))) {
                    datas.clear();
                    datas.addAll(response.getTagArray());
                    adapter.setDatas(datas);
                }
            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                ToastUtils.showShortToast(SearchDreamLabelActivity.this, response.getDesc());
            }
        });
    }

    @OnClick({R.id.tv_cancel_label, R.id.layout_back})
    public void doClicks(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.layout_back:
                App.GetInstance().finishActivity(SearchDreamLabelActivity.this);
                break;
            case R.id.tv_cancel_label:
                edt_query_label.setText("");
                datas.clear();
                adapter.setDatas(datas);
                break;
        }
    }
}
